<?php

class ProductosController extends AppController {

    public $helpers = array('Html', 'Form', 'Javascript', 'Ajax');
    var $components = array('RequestHandler', 'Auth', 'Cookie', 'Paginator');

    function beforeFilter() {

        $this->Auth->userModel = 'User';
        //$this->Auth->allow('updateRaiting', "sumarVisita");
        $this->Auth->fields = array('username' => 'username', 'password' => 'password');
        $this->Auth->loginAction = array('admin' => false, 'controller' => 'pages', 'action' => 'display');
        $this->Auth->loginRedirect = array('controller' => 'productos', 'action' => 'index');
    }

    public function admin_index() {

        $this->Paginator->settings = array('limit' => 8);
        $this->set('productos', $this->paginate('Producto'));
    }

    public function admin_view($id = null) {

        $this->set('producto', $this->Producto->findById($id));
    }

    public function admin_add() {

        if ($this->request->is('post')) {

            if (isset($this->request->data['Producto']['imagen'])) {

                $this->request->data['Producto']['imagen'] = $this->request->data['Producto']['imagen']['name'];
            }
            if (isset($this->request->data['Producto']['imagendestacada'])) {

                $this->request->data['Producto']['imagendestacada'] = $this->request->data['Producto']['imagendestacada']['name'];
            }
            if ($this->Producto->save($this->request->data)) {

                $this->Session->setFlash('El producto ha sido agregada.');
                $this->redirect(array('action' => 'index'));
            } else {

                $this->Session->setFlash('No se ha podido cargar el producto.');
            }
        }
        $this->set('cs', $this->Producto->Marca->find('list', array('fields' => array('Marca.nombre'))));
    }

    public function admin_edit($id = null) {

        if ($this->request->is('get')) {

            $this->request->data = $this->Producto->findById($id);
            $this->set('cs', $this->Producto->Marca->find('list', array('fields' => array('Marca.nombre'))));
        } else {

            if ($this->Producto->save($this->request->data)) {

                $this->Session->setFlash('El producto se ha  actualizado.');
                $this->redirect(array('action' => 'index'));
            } else {

                $this->Session->setFlash('No se ha podido agregar el producto.');
            }
        }
    }

    public function admin_delete($id) {

        if ($this->request->is('get')) {

            throw new MethodNotAllowedException();
        }
        if ($this->Producto->delete($id)) {

            $this->Session->setFlash('El producto con id: ' . $id . ' ha sido borrado.');
            $this->redirect(array('action' => 'index'));
        }
    }

    public function admin_updateRubro($option, $id) {

        if ($this->request->is('get')) {

            throw new MethodNotAllowedException();
        }

        if ($option == 3) { //si opcion es 3 entonces quito un director
            unset($this->request->data['Rubro'][$id]);

            $this->set('rubros', $this->Producto->Rubro->find('list', array('conditions' => array('NOT' => array('Rubro.id' => $this->request->data['Rubro'])), 'fields' => array('Rubro.nombre'))));
            $this->set('trubros', $this->Producto->Rubro->find('list', array('conditions' => array('Rubro.id' => $this->request->data['Rubro']), 'fields' => array('Rubro.nombre'))));
        } else {

            if (isset($this->request->data['rubros'])) { //si opcion no es 3 y se selecciono un director entonces agrego un director
                $id = $this->request->data['rubros'];

                $this->request->data['Rubro'][$id] = $id;

                $this->set('rubros', $this->Producto->Rubro->find('list', array('conditions' => array('NOT' => array('Rubro.id' => $this->request->data['Rubro'])), 'fields' => array('Rubro.nombre'))));
                $this->set('tRubros', $this->Producto->Rubro->find('list', array('conditions' => array('Rubro.id' => $this->request->data['Rubro']), 'fields' => array('Rubro.nombre'))));
            } else { //sino se inicializan los datos para crear (opcion 1) una pelicula o para editar (opcion 2) una pelicula
                if ($option == 1) {

                    $this->request->data['Rubro'] = array();
                    $listRubros = $this->Producto->Rubro->find('list', array('fields' => 'Rubro.nombre'));
                    $this->set('rubros', $listRubros);
                    $this->set('tRubros', array());
                } else {

                    $options['joins'] = array(
                        array('table' => 'rubros_productos',
                            'alias' => 'RubrosProductos',
                            'type' => 'inner',
                            'conditions' => array(
                                'Rubro.id = RubrosProductos.rubro_id'
                            )
                        ),
                        array('table' => 'productos',
                            'alias' => 'Producto',
                            'type' => 'inner',
                            'conditions' => array(
                                'RubrosProductos.producto_id =' . $id
                            )
                        )
                    );
                    $this->Producto->recursive = -1;
                    $aux = $this->Producto->Rubro->find('list', $options);
                    $this->Producto->recursive = 1;
                    $this->set('rubros', $this->Producto->Rubro->find('list', array('conditions' => array('NOT' => array('Rubro.id' => $aux)), 'fields' => array('Rubro.nombre'))));

                    $this->set('tRubros', $this->Producto->Rubro->find('list', array('conditions' => array('Rubro.id' => $aux), 'fields' => array('Rubro.nombre'))));
                    $this->request->data['Rubro'] = $this->Producto->Rubro->find('list', array('conditions' => array('Rubro.id' => $aux)));
                }
            }
        }
    }

    public function admin_updateLinea($option, $id) {

        if ($this->request->is('get')) {

            throw new MethodNotAllowedException();
        }

        if ($option == 3) { //si opcion es 3 entonces quito un director
            unset($this->request->data['Linea'][$id]);

            $this->set('lineas', $this->Producto->Linea->find('list', array('conditions' => array('NOT' => array('Linea.id' => $this->request->data['Linea'])), 'fields' => array('Linea.nombre'))));
            $this->set('tLineas', $this->Producto->Linea->find('list', array('conditions' => array('Linea.id' => $this->request->data['Linea']), 'fields' => array('Linea.nombre'))));
        } else {

            if (isset($this->request->data['Lineas'])) { //si opcion no es 3 y se selecciono un director entonces agrego un director
                $id = $this->request->data['Lineas'];

                $this->request->data['Linea'][$id] = $id;

                $this->set('lineas', $this->Producto->Linea->find('list', array('conditions' => array('NOT' => array('Linea.id' => $this->request->data['Linea'])), 'fields' => array('Linea.nombre'))));
                $this->set('tLineas', $this->Producto->Linea->find('list', array('conditions' => array('Linea.id' => $this->request->data['Linea']), 'fields' => array('Linea.nombre'))));
            } else { //sino se inicializan los datos para crear (opcion 1) una pelicula o para editar (opcion 2) una pelicula
                if ($option == 1) {

                    $this->request->data['Linea'] = array();
                    $listLineas = $this->Producto->Linea->find('list', array('fields' => 'Linea.nombre'));
                    $this->set('lineas', $listLineas);
                    $this->set('tLineas', array());
                } else {

                    $options['joins'] = array(
                        array('table' => 'lineas_productos',
                            'alias' => 'LineasProductos',
                            'type' => 'inner',
                            'conditions' => array(
                                'Linea.id = LineasProductos.linea_id'
                            )
                        ),
                        array('table' => 'productos',
                            'alias' => 'Producto',
                            'type' => 'inner',
                            'conditions' => array(
                                'LineasProductos.producto_id =' . $id //aca $id es el id del producto que se desea editar
                            )
                        )
                    );
                    $this->Producto->recursive = -1;
                    $aux = $this->Producto->Linea->find('list', $options);
                    $this->Producto->recursive = 1;
                    $this->set('lineas', $this->Producto->Linea->find('list', array('conditions' => array('NOT' => array('Linea.id' => $aux)), 'fields' => array('Linea.nombre'))));

                    $this->set('tLineas', $this->Producto->Linea->find('list', array('conditions' => array('Linea.id' => $aux), 'fields' => array('Linea.nombre'))));
                    $this->request->data['Linea'] = $this->Producto->Linea->find('list', array('conditions' => array('Linea.id' => $aux)));
                }
            }
        }
    }

}

?>